package com.store.bauhinia.order.service.impl;

import com.bookstore.bauhinia.mapper.OrderMapper;
import com.bookstore.bauhinia.pojo.Order;
import com.bookstore.bauhinia.pojo.OrderExample;
import com.store.bauhinia.order.service.OrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderMapper orderMapper;

    @Override
    public Order selectByPrimaryKey(Integer id) {
        return orderMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKey(Order record) {
        return orderMapper.updateByPrimaryKey(record);
    }

    @Override
    public int insert(Order record) {
        record.setCreateTime(new Date());
        return orderMapper.insertSelective(record);
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return orderMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Order> getAllOrders() {
        return orderMapper.selectByExample(new OrderExample());
    }

    @Override
    public List<Order> getOrdersByUserId(Integer userId) {
        OrderExample example = new OrderExample();
        OrderExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        return orderMapper.selectByExample(example);
    }
}
